package br.com.po1.ex5;

import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JOptionPane;

public class Helper {
	
	public static String read(String msg){
		return JOptionPane.showInputDialog(msg);
	}
	public static String read(String msg, String title, int messageType){
		return JOptionPane.showInputDialog(null, msg, title, messageType);
	}
	public static String read(String msg, String title, int messageType, Object[] selectionValues, int initialSelectionValue){
		return JOptionPane.showInputDialog(null, msg, title, messageType, null, selectionValues, selectionValues[initialSelectionValue]).toString();
	}
	public static void write(String msg){
		JOptionPane.showMessageDialog(null, msg);
	}
	public static void write(Object obj){
		JOptionPane.showMessageDialog(null, obj);
	}
	public static void write(String title, String msg, int messageType){
		JOptionPane.showMessageDialog(null, msg, title, messageType);
	}
	public static void write(String title, Object obj, int messageType){
		JOptionPane.showMessageDialog(null, obj, title, messageType);
	}
	public static String formatDate(Date date, String format){
		return new SimpleDateFormat(format).format(date);
	}
	public static Date toDate(String date, String format){
		Date data = null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			data = sdf.parse(date);
			return data;
		} catch (ParseException e) {
			System.out.println(e.getMessage());
			JOptionPane.showMessageDialog(null, "Data incorreta");
		}
		return data;
	}
	public static Calendar toCalendar(String date, String format){
		Calendar cal = Calendar.getInstance();
		cal.setTime(toDate(date, format));
		return cal;
	}
	public static String calendarToString(Calendar cal, String format){
		Date date = cal.getTime();
		return formatDate(date, format);
	}
}
